package cn.josewu;


import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.http.HttpHost;
import org.junit.Test;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: covid19
 * @description:
 * @author: Mr. Tigger
 * @create 2020-10-11 22:35
 **/
public class HttpClientTest {
    @Test
    public  void   testGet() throws Exception {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet("http://www.itcast.cn/?username=java");
        httpGet.setHeader("User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36");
        CloseableHttpResponse response = httpclient.execute(httpGet);

        if(response.getStatusLine().getStatusCode() ==200) {
            String html = EntityUtils.toString(response.getEntity(),"UTF-8");
            System.out.println(html);
        }

        httpclient.close();
        response.close();

    }

    @Test
    public void testPost() throws Exception {
        CloseableHttpClient    httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost("http://www.itcast.cn/");
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("username","java"));
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params,"UTF-8");
        httpPost.setEntity(entity);
        httpPost.setHeader("User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36");
        CloseableHttpResponse response = httpClient.execute(httpPost);

        if( response.getStatusLine().getStatusCode() == 200 ){
            String html = EntityUtils.toString(response.getEntity(),"UTF-8");
            System.out.println(html);

        }
        response.close();
        httpClient.close();

    }

    @Test
    public  void    testPool() throws Exception {
        PoolingHttpClientConnectionManager cm =new  PoolingHttpClientConnectionManager();
        cm.setMaxTotal(200);    //设置最大连接数
        cm.setDefaultMaxPerRoute(20);  //设置每个主机的最大并发
        doGet(cm);
        doGet(cm);
    }

    private void doGet(PoolingHttpClientConnectionManager cm) throws Exception {
        CloseableHttpClient  httpClient = HttpClients.custom().setConnectionManager(cm).build();
        HttpGet httpGet = new HttpGet("http://www.itcast.cn/");
        CloseableHttpResponse response= httpClient.execute(httpGet);
        if(response.getStatusLine().getStatusCode() == 200) {
            String html = EntityUtils.toString(response.getEntity());
            System.out.println(html);
        }
        response.close();
    }

    @Test
    public  void testConfig() throws Exception{
        RequestConfig requestConfig = RequestConfig.custom()
                .setSocketTimeout(10000)
                .setConnectTimeout(10000)
                .setConnectionRequestTimeout(10000)
                .setProxy(new HttpHost("118.24.172.149",1080))
                .build();
        CloseableHttpClient  httpClient = HttpClients.custom().setDefaultRequestConfig(requestConfig).build();
        HttpGet httpGet = new HttpGet("http://www.itcast.cn/");
        CloseableHttpResponse response= httpClient.execute(httpGet);
        if(response.getStatusLine().getStatusCode() == 200) {
            String html = EntityUtils.toString(response.getEntity());
            System.out.println(html);
        }
        response.close();

    }
}
